# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Display the icons used in DataZoomer"""

from dzapp import *
from os import listdir
from dzpage import Page

class IconApp(App):
    icon_height = 25

    def FormatIcon(self,filename,dirname):
        icon= DataZoomerIcon(filename,dirname)
        return '<table border=0 height=64 width=64><tr><td><center>%s</center></td></tr><tr><td><center>%s</center></td></tr></table>' % (icon.aslink(),icon.asname())

    def GenerateIconList(self,title,icon_dir):
        icons = [filename for filename in listdir(icon_dir) if filename[-3:] == 'png']

        if len(icons)==0: return None

        isize = 30
        max = 5
        pct = 100/max

        count = 0
        content = ''

        for icon in icons:
            if count == max:
                count = 0
                content += '</tr><tr>'
            content += '<td height=%s width=%d%% align=center><center>\n<img height=%s border="0" src="images/%s/%s"><br>\n%s</center></td>' % \
                (isize*2,pct,isize,'icons',icon,icon.replace('.png',''))
            count += 1

        while count < max:
            content += '<td width=%d%% align=center><center>&nbsp;</center></td>' % pct
            count += 1

        content = '<table border=0 width=100%% cellspacing=5><tr>%s</tr></table>' % (content)

        return content

    def _view_(self):
        content = self.GenerateIconList('Site Icons','%s/images/icons'%zoomer.site_path)
        return Page(content)

result = IconApp('icons')
